import axios from "axios"
import auth from "./auth"
 

const BASE_URL = "http://127.0.0.1:8000"
// const BASE_URL = "http://118.24.0.251:8000"


class Http {
  constructor() {
    this.http = axios.create({
      baseURL: BASE_URL
    })

    // 设置拦截器，用来添加JWT Token的
    this.http.interceptors.request.use(config => {
      const token = auth.token;
      if (token && token != 'undefined') {
        config.headers.Authorization = "JWT " + token
      }
      return config
    })

    // 设置拦截器，如果返回403，跳转到登陆页面
    // 403是没有权限访问这个接口
    this.http.interceptors.response.use(null, err => {
      if (err && err.response && err.response.status == 403) {
        auth.clearUserToken()
      }
      return Promise.reject(err);
    })
  }

  //定义登录请求
  login(params) {
    const url = "/auth/login"
    return this.http.post(url, params)
  }
  //添加项目请求
  addProject(params){
    const url = "/project"
    return this.http.post(url,params)
  }
  // 获取项目列表
  getProjectList(){
    const url = "/project"
    return this.http.get(url)
  }
  // 编辑项目
  editProject(project_id,params){
    const url = "/project/" + project_id
    return this.http.put(url,params)
  }
  // 删除项目
  delProject(project_id){
    const url = "/project/" + project_id
    return this.http.delete(url)
  }
  //获取项目详情
  getProjectDetail(project_id){
    const url = "/project/" + project_id
    return this.http.get(url)
  }
  //添加host
  addHost(params){
    const url = "/host"
    return this.http.post(url,params)
  }
  // 编辑host
  editHost(host_id,params){
    const url = "/host/" + host_id
    return this.http.put(url,params)
  }
  //删除host
  deleteHost(host_id){
    const url = "/host/" + host_id
    return this.http.delete(url)
  }
  // 添加api
  addApi(params){
    const url = "/api"
    return this.http.post(url,params)
  }
  //删除api
  deleteApi(api_id){
    const url = "/api/" + api_id
    return this.http.delete(url)
  }
  // 编辑api
  editApi(api_id,params){
    const url = "/api/" + api_id
    return this.http.put(url,params)
  }
  //运行api
  runApi(api_id){
    const url = "/run/api/" + api_id
    return this.http.post(url)
  }
  // 添加case
  addCase(params){
    const url = "/case"
    return this.http.post(url,params)
  }
  //编辑case
  editCase(case_id,params){
    const url = "/case/" + case_id
    return this.http.put(url,params)
  }
  // 运行case方法
  runCase(case_id){
    const url = "/run/case/" + case_id
    return this.http.post(url)
  }

 //获取运行记录
  getRunRecords(project_id,type){
    const url = "/record?project=" + project_id + "&type=" + type
    return this.http.get(url)
  }
  // 编辑用户信息
  editUser(params){
    const url = "/auth/user"
    return this.http.put(url,params)
  }
  //获取用户列表
  getUserList(){
    const url = "/auth/user"
    return this.http.get(url)
  }
  //获取首页信息
  getIndexData(){
    const url = "/index"
    return this.http.get(url)
  }
  //新增任务
  addTask(params){
    const url = "/task"
    return this.http.post(url,params)
  }
  //编辑任务
  editTask(task_id,params){
    const url = "/task/" + task_id
    return this.http.put(url,params)
  }
  //修改任务状态
  changeTaskStatus(task_id,target_status){
    const url = "/task/" + task_id + "/" + target_status
    return this.http.post(url)
  }
}

export default new Http()